package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.ui.adapter.MsgAdapter;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.MessageModel;
import com.shd.edanpei.presenter.contract.MessageContract;
import com.shd.edanpei.presenter.MessagePresenter;
import com.shd.edanpei.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/3/29.
 * 我的消息
 */
public class MessageActivity extends BaseActivity<MessagePresenter>
        implements MessageContract.MessageIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.rlv_message)
    RecyclerView recyclerView;

    private MsgAdapter mAdapter;

    @Override
    protected int setLayout() {
        return R.layout.activity_mes;
    }

    @Override
    protected MessagePresenter bindPresenter() {
        return new MessagePresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("平台消息");
        mAdapter = new MsgAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
        mPresenter.refresh();


    }

    @Override
    protected void initListener() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adaapter, View view, int position) {

                Bundle bundle = new Bundle();
                bundle.putString(Constant.FROM, "1");
                bundle.putParcelable(Constant.DATA, mAdapter.getItem(position));
                startActivity(DynamicsDetailsActivity.class, bundle);
                getMessage(mAdapter.getItem(position).getId(), position);
            }
        });
    }


    private void getMessage(final int id, final int index) {
        ApiCreator.getApi().messageDetails(id)
                .compose(RxTransformer.<MessageModel>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MessageModel>() {
                    @Override
                    protected void onSuccess(MessageModel data) {
                        mAdapter.getItem(index).setStatus(false);
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    @OnClick(R.id.iv_headline_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public MsgAdapter getAdapter() {
        return mAdapter;
    }
}
